package com.bj58.groupbuy.action.service.impl;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.bj58.groupbuy.action.db.CommonDao;
import com.bj58.groupbuy.action.db.DBConfig;
import com.bj58.groupbuy.action.model.PCSource;
import com.bj58.groupbuy.action.service.PCSourceService;
import com.bj58.groupbuy.action.util.DateUtil;
import com.bj58.groupbuy.util.PathUtil;

public class PCSourceServiceImpl implements PCSourceService{

	private CommonDao comDao = new CommonDao();
	@Override
	public List<PCSource> getAllList() throws Exception {
		String sql="select * from data_dict.PC_Source";
		return comDao.getAllColsByVo(sql, PCSource.class, DBConfig.db_94_data_app.getIntType());
	}

	@Override
	public boolean dealPCSourceData(List<String> ecList) throws Exception {
		HashMap<String,String> msMap=new HashMap<String,String>();
		//上传的数据
		for(String line:ecList){
			line=line.replaceAll("@", "\t");
			if(line!=null && !"".equals(line.trim())){
				String recode="";
				String[] field=line.split("\t");
				for(int i=0;i<5;i++){
					String f="";
					if(i<field.length)
						f=field[i].trim();
					recode+=f;
					if(i<4)
						recode+="\t";
				}
				String key = "";
				if(field.length==4)
					 key = field[3]+"_";
				else if(field.length==5)
					key = field[3]+"_"+field[4];
				msMap.put(key, recode);
			}
		}
		List<PCSource> msList=getAllList();
		//数据库中已有的数据
		for(PCSource ms:msList){
			String code=ms.getCode1()+"_";
			if(ms.getCode2() !=null)
				code += ms.getCode2();
			if(!msMap.containsKey(code)){
				String line=ms.getSource1()+"\t"+ms.getSource2()+"\t"+ms.getSource3()+"\t"+ms.getCode1()+"\t"+ms.getCode2();
				msMap.put(code,line);
			}
		}
		String path = PathUtil.getWFNamespaceFolder()+"/pc_source_data/";
		 File dirFile = new File(path);
		if(!dirFile.exists())
			dirFile.mkdirs();
//		FileUtil.deleteDirectoryNoSefl(path);//清空目录下所有数据
		path+="pc_source_"+DateUtil.getStringDate(new Date(), "yyyyMMdd_HH-mm-ss");
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),"UTF-8"));
		Iterator<String> iter=msMap.keySet().iterator();
		while(iter.hasNext()){
			String code=iter.next();
			String line=msMap.get(code);
			writer.write(line+"\n");
		}
		writer.close();
		String dropSql="drop table data_dict.PC_Source;";
		String createSql="create table data_dict.PC_Source(" +
		  "`source1` varchar(100) DEFAULT NULL COMMENT '一级来源',"+
		  "`source2` varchar(100) DEFAULT NULL COMMENT '二级来源',"+
		  "`source3` varchar(100) DEFAULT NULL COMMENT '三级来源',"+
		  "`code1` varchar(100) DEFAULT NULL COMMENT '代码标识1',"+
		  "`code2` varchar(100) DEFAULT NULL COMMENT '代码标识2'"+
		 ")ENGINE=BRIGHTHOUSE DEFAULT CHARSET=utf8;";
		String loadsql="load data local infile '"+path+"'  into table data_dict.PC_Source fields terminated by '\t';";

		List<String> sqlList=new ArrayList<String>();
		sqlList.add(dropSql);
		sqlList.add(createSql);
		sqlList.add(loadsql);
		return comDao.executeSql(sqlList, DBConfig.db_94_data_app.getIntType());
	}

	
}
